import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
//import java.util.Iterator;
import java.util.Map;
//import java.util.Set;

import javax.swing.JToggleButton;

public class Impressao {

	private boolean isUsed = false;
	int cont=1;
	Map<Object,String> mp=new HashMap<Object, String>();
	ArrayList<String> Tile = new ArrayList<String>();

	public Impressao(ViewerMapaMain vmp, ViewerOverMap vom, File OutputFile)
			throws IOException {

		FileWriter arq = new FileWriter(OutputFile);
		PrintWriter gravarArq = new PrintWriter(arq);

		gravarArq.printf("tileset:"
				+ Utils.getChoose().getSelectedFile().getName() + "%n");
		gravarArq.printf("basemap:%n");
		// System.out.println(vmp);
		gravarArq.printf("{(" + vmp.btnmap.length + "x" + vmp.btnmap[0].length
				+ ")%n");

		for (int i = 0; i < vmp.btnmap.length; i++) {
			for (int x = 0; x < vmp.btnmap[i].length; x++) {
				if (vmp.btnmap[i][x].getToolTipText().equals("VMP")) {
					gravarArq.printf(piso(vmp.btnmap[i][x]) + ":" + "00" + " ");
				} else {
					gravarArq.printf(piso(vmp.btnmap[i][x]) + ":"
							+ vmp.btnmap[i][x].getToolTipText() + " ");
				}
			}
			gravarArq.printf("%n");
		}
		gravarArq.printf("}%n");
		gravarArq.printf("portals:%n{%n");
		for (String portal : vmp.m.nomenclaturaPortal) {
			// System.out.println(portal);
			gravarArq.printf(portal);
		}

		gravarArq.printf("%n}%novermap:%n{%n");
		// System.out.println(vom.btnmap);

		for (String data : vom.m.nomenclaturaOverMap) {
			vom.m.YOverMap.add((String) data.subSequence(0, 1));
			vom.m.XOverMap.add((String) data.subSequence(2, 3));
			
		}
		
		for (String dado : vom.m.nomeTile) {
			Tile.add((String) dado.subSequence(2, 4));
			//System.out.println((String) dado.subSequence(2, 4));
		}
		for (int i = 0; i < vom.m.YOverMap.size(); i++) {
			//System.out.println(vom.m.YOverMap.get(i));
			int contY = Integer.parseInt(vom.m.YOverMap.get(i));// 2 //4
			if (isUsed) {
				contY = contY - 1 - Integer.parseInt(vom.m.YOverMap.get(i - 1));
			}
			for (int k = 0; k < contY; k++) {
				if(isUsed){
					gravarArq.printf("%n-");
				}else{
					gravarArq.printf("-%n");
				}
			}
			if(isUsed && vom.m.YOverMap.get(i).equals(vom.m.YOverMap.get(i-1)) ){
				cont++;
				mp.put(new Integer(Integer.parseInt(vom.m.YOverMap.get(i))), String.valueOf(cont));
				
			}else if (isUsed && !vom.m.YOverMap.get(i).equals(vom.m.YOverMap.get(i-1))){
				cont = 1;
				mp.put(new Integer(Integer.parseInt(vom.m.YOverMap.get(i))), String.valueOf(cont));
			}
			mp.put(new Integer(Integer.parseInt(vom.m.YOverMap.get(i))), String.valueOf(cont));
			
			if(!isUsed){
				gravarArq.printf(vom.m.XOverMap.get(i) + ":"
					+ Tile.get(i) + " ");
				System.out.println(vom.m.tBtns[i].getToolTipText() + " AND " + vom.m.tBtn.getToolTipText());
			}
			if(isUsed && vom.m.YOverMap.get(i).equals(vom.m.YOverMap.get(i-1))){
			gravarArq.printf(vom.m.XOverMap.get(i) + ":"
					+ Tile.get(i) + " ");
			}
			if(isUsed && !vom.m.YOverMap.get(i).equals(vom.m.YOverMap.get(i-1))){
				gravarArq.printf( "%n" + vom.m.XOverMap.get(i) + ":"
						+ Tile.get(i)+ " ");
				}

			isUsed = true;
		}
//		Set s=mp.entrySet();
//        Iterator it=s.iterator();
//        
//		for (int i = 0; i < vom.m.YOverMap.size(); i++) {
//
//		        if(it.hasNext())
//		        {
//		            // key=value separator this by Map.Entry to get key and value
//		            Map.Entry m =(Map.Entry)it.next();
//
//		            // getKey is used to get key of Map
//		            int key=(Integer)m.getKey();
//
//		            // getValue is used to get value of key in Map
//		            String value=(String)m.getValue();
//
//		            System.out.println("Key :"+key+"  Value :"+value);
//		        }
//		}
		gravarArq.printf("%n}");
		vom.m.nomenclaturaOverMap.clear();
		vom.m.XOverMap.clear();
		vom.m.YOverMap.clear();
		vom.m.RefazerVOM();
		isUsed = false;
		arq.close();
	}

	public Impressao(ViewerOverMap vom, File outputFile) {

	}

	public String piso(JToggleButton jtb) {
		if (jtb.isSelected()) {
			return "0";
		} else if (jtb.getFont().isItalic()) {
			return "2";
		}
		return "1";
	}

}
